package com.starbaba.core.web.translate;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starbaba.core.spring.interceptor.MethodCacheInterceptor;
import com.starbaba.core.utils.EhcacheManager;

/**
 * 使用装饰模式（Decorator），对TranslateUtils的方法进行封装，优先使用缓存，然后才查询数据
 * TranslateUtils
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2014-10-9 上午11:14:52
 * @version V1.0
 */
@SuppressWarnings("unchecked")
public class TranslateUtilsCacheDecorator {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private TranslateUtils target;
    
	/**
	 * 待包装的类名（全路径）
	 */
	private String thisOrginClassName;
	private Cache cache;
	
    public TranslateUtilsCacheDecorator(TranslateUtils target) {
        this.target = target;
        
        thisOrginClassName = target.getClass().getName();
		cache = EhcacheManager.getCache(thisOrginClassName);
    }

    /**
     * 根据definition进行翻译。优先使用缓存，然后才查询数据库
     * 
     * @param definition
     * @param value
     * @return
     *
     * @Copyright: Copyright (c) 2006
     * @Company: Revenco Tech Ltd.
     * @anthor chenminhui<chenminhui@revenco.com>
     * @since 2014-10-9 上午11:10:54
     * @version V1.0
     */
    public String getName(String definition, String value){
    	//优先从缓存获取数据
		//TODO 待实现
    	String cacheKey = null;
    	/*
    	String cacheKey = getCacheKey("getName", new Object[]{definition, value});
		if(cache != null) {
			Element cacheEle = cache.get(cacheKey);
			if(cacheEle != null) {
				logger.info("优先从缓存中获取翻译数据：definition=" + definition + ", value=" + value);
				return (String) cacheEle.getObjectValue();
			}
		}
		*/
		//缓存没有数据，查询数据库
		String rtnValue = target.getName(definition, value);
		if(cache != null) {
			cache.put(new Element(cacheKey, rtnValue));
		}
		return rtnValue;
	}
	
    /**
     * 查询系统参数。优先使用缓存，然后才查询数据库
     * @param type 列表
     * @param value 值
     * @param propertyCode 属性编码
     * @param propertyName 属性值
     * @param isReturnOrginValue 结果为空时，是否需要返回value
     * @return String
     * @throws Exception
     *
     * @Copyright: Copyright (c) 2006
     * @Company: Revenco Tech Ltd.
     * @anthor chenminhui<chenminhui@revenco.com>
     * @since 2014-10-9 上午11:16:15
     * @version V1.0
     */
    public String getSyscodeName(String type, String value, String propertyCode, String propertyName, boolean isReturnOrginValue) throws Exception{
    	//优先从缓存获取数据
		String cacheKey = getCacheKey("getSyscodeName", new Object[]{type, value, propertyCode, propertyName, isReturnOrginValue});
		if(cache != null) {
			Element cacheEle = cache.get(cacheKey);
			if(cacheEle != null) {
				logger.info("优先从缓存中获取翻译数据：type=" + type + ", value=" + value + ", propertyCode=" + propertyCode + ", propertyName=" + propertyName + ", isReturnOrginValue=" + isReturnOrginValue);
				return (String) cacheEle.getObjectValue();
			}
		}
		
		//缓存没有数据，查询数据库
		String rtnValue = target.getSyscodeName(type, value, propertyCode, propertyName, isReturnOrginValue);
		if(cache != null) {
			cache.put(new Element(cacheKey, rtnValue));
		}
		return rtnValue;
    }
    
    /**
	 * 获取列表信息
	 * @param @param definition
	 * @param @param listKey
	 * @param @param listValue
	 * @param @param needSpace
	 * @return List<TranslateVO>
	 * @throws
	 */
	public List<TranslateVO> getCollection(String definition) {
		//优先从缓存获取数据
		String cacheKey = getCacheKey("getCollection", new Object[]{definition});
		if(cache != null) {
			Element cacheEle = cache.get(cacheKey);
			if(cacheEle != null) {
				logger.info("优先从缓存中获取翻译数据：definition=" + definition);
				return (List<TranslateVO>) cacheEle.getObjectValue();
			}
		}
		
		//缓存没有数据，查询数据库
		List<TranslateVO> rtnValue = target.getCollection(definition);
		if(cache != null) {
			cache.put(new Element(cacheKey, rtnValue));
		}
		return rtnValue;
	}
	
    /**
     * 获取缓存的Key名称
     * @param methodName 调用方法名
     * @param args 调用参数列表
     * @return String 
     *
     * @Copyright: Copyright (c) 2006
     * @Company: Revenco Tech Ltd.
     * @anthor chenminhui<chenminhui@revenco.com>
     * @since 2014-10-9 上午11:22:58
     * @version V1.0
     */
    private String getCacheKey(String methodName, Object[] args) {
    	return MethodCacheInterceptor.getCacheKey(thisOrginClassName, methodName, args);
    }
}
